package com.ruoyi.quality.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quality.domain.dto.TQualityBizRectifyReviewDTO;
import com.ruoyi.safe.domain.BizExtensionRequests;
import com.ruoyi.safe.domain.BizRectifyReview;
import com.ruoyi.safe.domain.dto.BizRectifyReviewDTO;
import com.ruoyi.safe.service.IBizExtensionRequestsService;
import com.ruoyi.safe.service.IBizRectifyReviewService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quality.domain.TQualityFileItem;
import com.ruoyi.quality.service.ITQualityFileItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 质量问题拆分明细Controller
 * 
 * @author mahl2
 * @date 2024-02-05
 */
@RestController
@RequestMapping("/quality/qualityFileItem")
public class TQualityFileItemController extends BaseController
{
    @Autowired
    private ITQualityFileItemService tQualityFileItemService;
    @Autowired
    private IBizRectifyReviewService bizRectifyReviewService;
    @Autowired
    private IBizExtensionRequestsService bizExtensionRequestsService;

    /**
     * 查询质量问题拆分明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TQualityFileItem tQualityFileItem)
    {
        startPage();
        List<TQualityFileItem> list = tQualityFileItemService.selectTQualityFileItemList(tQualityFileItem);
        return getDataTable(list);
    }

    /**
     * 查询质量问题我的待办列表
     */
    @GetMapping("/selectCheckLedger")
    public TableDataInfo selectCheckLedger(TQualityFileItem tQualityFileItem)
    {
        startPage();
        List<TQualityFileItem> list = tQualityFileItemService.selectCheckLedger(tQualityFileItem);
        return getDataTable(list);
    }

    /**
     * 查询质量问题操作明细-前端要的操作详情
     */
    @GetMapping("/selectReviewDetail")
    public AjaxResult selectReviewDetail(Long id)
    {
        return success(tQualityFileItemService.selectReviewDetail2(id));
    }

    /**
     * 查询复查明细
     */
    @GetMapping("/selectReviewCheck")
    public AjaxResult selectReviewCheck(Long id)
    {
        return success(tQualityFileItemService.selectReviewCheck(id));
    }

    /**
     * 查询明细-整改
     */
    @GetMapping("/selectChildReview")
    public AjaxResult selectChildReview(Long id, Long recordId)
    {
        return success(tQualityFileItemService.selectChildReview(id, recordId));
    }

    /**
     * 查询明细-复查
     */
    @GetMapping("/selectChildReview2")
    public AjaxResult selectChildReview2(Long id, Long recordId)
    {
        return success(tQualityFileItemService.selectChildReview2(id, recordId));
    }

    /**
     * 查询明细-延期申请
     */
    @GetMapping("/selectChildCheck")
    public AjaxResult selectChildCheck(Long id, Long recordId)
    {
        return success(tQualityFileItemService.selectChildCheck(id, recordId));
    }

    /**
     * 查询明细-移交
     */
    @GetMapping("/selectChildTransfer")
    public AjaxResult selectChildTransfer(Long id, Long recordId)
    {
        return success(tQualityFileItemService.selectChildTransfer(id, recordId));
    }

    /**
     * 查询质量问题我的审核列表
     */
    @GetMapping("/selectExtensionLedger")
    public TableDataInfo selectExamineLedger(TQualityFileItem tQualityFileItem)
    {
        startPage();
        List<TQualityFileItem> list = tQualityFileItemService.selectExtensionLedger(tQualityFileItem);
        return getDataTable(list);
    }

    /**
     * 导出质量问题拆分明细列表
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFileItem:export')")
    @Log(title = "质量问题拆分明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TQualityFileItem tQualityFileItem)
    {
        List<TQualityFileItem> list = tQualityFileItemService.selectTQualityFileItemList(tQualityFileItem);
        ExcelUtil<TQualityFileItem> util = new ExcelUtil<TQualityFileItem>(TQualityFileItem.class);
        util.exportExcel(response, list, "质量问题拆分明细数据");
    }

    /**
     * 获取质量问题拆分明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFileItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tQualityFileItemService.selectTQualityFileItemById(id));
    }

    /**
     * 新增质量问题拆分明细
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFileItem:add')")
    @Log(title = "质量问题拆分明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TQualityFileItem tQualityFileItem)
    {
        return toAjax(tQualityFileItemService.insertTQualityFileItem(tQualityFileItem));
    }

    /**
     * 修改质量问题拆分明细
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFileItem:edit')")
    @Log(title = "质量问题拆分明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TQualityFileItem tQualityFileItem)
    {
        return toAjax(tQualityFileItemService.updateTQualityFileItem(tQualityFileItem));
    }

    /**
     * 删除质量问题拆分明细
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFileItem:remove')")
    @Log(title = "质量问题拆分明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tQualityFileItemService.deleteTQualityFileItemByIds(ids));
    }


    /**
     * 新增整改
     */
    @Log(title = "复查--整改", businessType = BusinessType.INSERT)
    @PostMapping("/addRectifyReview")
    public AjaxResult addRectifyReview(@RequestBody TQualityBizRectifyReviewDTO tQualityBizRectifyReview) {

        if (tQualityBizRectifyReview.getListBizRectifyReview().size() <= 0) {
            throw new RuntimeException("列表信息不能为空");
        }
        for (BizRectifyReviewDTO bizRectifyReview : tQualityBizRectifyReview.getListBizRectifyReview())
        {
            if (ObjectUtil.isNull(bizRectifyReview.getProjectId())) {
                throw new RuntimeException("项目id不能为空");
            }
//            if (ObjectUtil.isEmpty(bizRectifyReview.getOperatorExplain()) && bizRectifyReview.getOperatorType().equals("1")) {
//                throw new RuntimeException("复查说明不能为空");
//            }
//            if (ObjectUtil.isEmpty(bizRectifyReview.getOperatorExplain()) && bizRectifyReview.getOperatorType().equals("2")) {
//                throw new RuntimeException("整改说明不能为空");
//            }
        }

        return toAjax(tQualityFileItemService.addRectifyReview(tQualityBizRectifyReview));
    }

    /**
     * 新增复查
     */
    @Log(title = "复查", businessType = BusinessType.INSERT)
    @PostMapping("/addRectifyCheck")
    public AjaxResult addRectifyCheck(@RequestBody BizRectifyReviewDTO bizRectifyReview) {
        if (ObjectUtil.isNull(bizRectifyReview.getProjectId())) {
            throw new RuntimeException("项目id不能为空");
        }
//        if (ObjectUtil.isEmpty(bizRectifyReview.getOperatorExplain()) && bizRectifyReview.getOperatorType().equals("1")) {
//            throw new RuntimeException("复查说明不能为空");
//        }
//        if (ObjectUtil.isEmpty(bizRectifyReview.getOperatorExplain()) && bizRectifyReview.getOperatorType().equals("2")) {
//            throw new RuntimeException("整改说明不能为空");
//        }

        return toAjax(tQualityFileItemService.addRectifyReview(bizRectifyReview));
    }

    /**
     * 申请延期
     */
    @PutMapping("/apply")
    public AjaxResult apply(@RequestBody BizExtensionRequests bizExtensionRequests) {
        return toAjax(bizExtensionRequestsService.insertBizExtensionRequests(bizExtensionRequests));
    }

    /**
     * 审核申请延期
     */
    @PutMapping("/accept")
    public AjaxResult accept(@RequestBody BizExtensionRequests bizExtensionRequests)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String currentUsername = loginUser.getNickname();;
        if (bizExtensionRequests.getStatus().equals("2"))
        {
            TQualityFileItem tQualityFileItem = new TQualityFileItem();
            tQualityFileItem.setId(bizExtensionRequests.getRecordId());
            tQualityFileItem.setReviseTheDates(bizExtensionRequests.getExtensionTime());
            tQualityFileItemService.updateTQualityFileItem(tQualityFileItem);
        }
        bizExtensionRequests.setReplyUser(currentUsername);
        bizExtensionRequests.setReplyTime(DateUtils.getNowDate());
        return toAjax(bizExtensionRequestsService.updateBizExtensionRequests(bizExtensionRequests));
    }
}
